import { Injectable } from '@angular/core';
import { Http, Headers } from "@angular/http";
import { Tag } from "../entity/tag.model";
import { Observable } from "rxjs/Observable";
import { Subject } from "rxjs/subject";
import { AppConfig } from "../config";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class TagService {
  private param = "tags";
  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(private http: InterceptorService) { }

  /**
   * 根据 tagSet获得测点集合
   * 
   * @param {string} tagSet 
   * @returns 
   * @memberof TagService
   */
  getTagsByTagSet(tagSet: string): Promise<Tag[]> {
    return this.http.get(`${this.url}/tagSet/${tagSet}`)
      .toPromise()
      .then(response => {
        return response.json().data as Tag[]
      })
      .catch(this.handleError);
  }
  /**
   * 
   * 
   * @returns {Promise<Tag[]>} 
   * @memberof TagService
   */
  getTagList(): Promise<Tag[]> {  
    return this.http.get(this.url)
      .toPromise()
      .then(response => {
        return response.json().data as Tag[]
      })
      .catch(this.handleError);
  }
  getSelectorTags(tagset: string, keyIds: number[]): Promise<Tag[]> {
    return this.http.get(`${AppConfig.APP_RESTFUL}/models/tagmodel/tagset/${tagset}?items=${keyIds}`)
      .toPromise()
      .then(response => {
        return response.json().data as Tag[];
      })
      .catch(this.handleError);
  }
  getTagNameTagList(tagCode: string, tagName: string): Promise<Tag[]> {
    const url = `${this.url}/${tagCode}/${tagName}`;
    return this.http.get(url)
      .toPromise()
      .then(response => response.json().data as Tag[])
      .catch(this.handleError);
  }
  create(tag: Tag): Promise<Tag> {
    return this.http
      .post(this.url, JSON.stringify(tag))
      .toPromise()
      .then(res => res.json().data)
      .catch(this.handleError);
  }
  update(tag: Tag): Promise<Tag> {
    const url = `${this.url}`;
    return this.http
      .put(url, JSON.stringify(tag))
      .toPromise()
      .then(() => tag)
      .catch(this.handleError);
  }
  delete(id: number): Promise<void> {
    const url = `${this.url}/${id}`;
    return this.http.delete(url)
      .toPromise()
      .then(() => null)
      .catch(this.handleError);
  }
  batchDelete(keyIds: number[]): Promise<boolean> {
    const apiUrl = `${this.url}?items=${keyIds}`;
    return this.http.delete(apiUrl)
      .toPromise()
      .then(response => {
        try {
          let result = response.json();
          switch (result.code) {
            case "200":
              return true;
            case "-1":
              throw new Error(result.message);
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }
  hasTagName(statusVal: string, tagId: number, name: string): Observable<boolean> {
    const url = `${this.url}/${name}/${statusVal}/tagId/${tagId}`;
    return this.http.get(url).map(response => {
      let code = response.json().code;
      switch (code) {
        case "3001":
          return true;
        case "3002":
          return false;
      }

    }).catch(this.handleError);
  }
  private handleError(error: any): Promise<any> {
    // console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }

}



// WEBPACK FOOTER //
// ./src/app/service/tag.service.ts